import scala.util.Random

// 定义音乐类
case class Music(name: String, artist: String)

// 定义播放模式枚举
object PlayMode extends Enumeration {
  type PlayMode = Value
  val Sequential, Shuffle, RepeatOne = Value
}

import PlayMode._

class MusicPlayer {
  private var musicList: List[Music] = Nil
  private var currentMusic: Option[Music] = None
  private var playMode: PlayMode = Sequential

  // 添加音乐到列表
  def addMusic(music: Music): Unit = {
    musicList = music :: musicList
    println(s"Added music: ${music.name} by ${music.artist}")
  }

  // 从列表中删除音乐
  def removeMusic(music: Music): Unit = {
    musicList = musicList.filterNot(_ == music)
    println(s"Removed music: ${music.name} by ${music.artist}")
  }

  // 播放音乐
  def play(): Unit = {
    playMode match {
      case Sequential =>
        currentMusic match {
          case Some(music) =>
            println(s"Playing ${music.name} by ${music.artist}")
          case None =>
            println("No music to play.")
        }
      case Shuffle =>
        if (musicList.nonEmpty) {
          val randomIndex = Random.nextInt(musicList.length)
          currentMusic = Some(musicList(randomIndex))
          println(s"Playing ${currentMusic.get.name} by ${currentMusic.get.artist}")
        } else {
          println("Music list is empty.")
        }
      case RepeatOne =>
        currentMusic match {
          case Some(music) =>
            println(s"Playing ${music.name} by ${music.artist} on repeat.")
          case None =>
            println("No music to play.")
        }
    }
  }

  // 切换播放模式
  def changePlayMode(mode: PlayMode): Unit = {
    playMode = mode
    println(s"Changed play mode to $playMode")
  }
}

object Main extends App {
  // 创建音乐播放器
  val player = new MusicPlayer()

  // 添加一些音乐到播放器
  player.addMusic(Music("Song1", "Artist1"))
  player.addMusic(Music("Song2", "Artist2"))
  player.addMusic(Music("Song3", "Artist3"))

  // 设置播放模式为随机
  player.changePlayMode(Shuffle)

  // 播放音乐
  player.play()

  // 从播放器中移除音乐
  player.removeMusic(Music("Song2", "Artist2"))

  // 播放音乐
  player.play()
} 